package models.admin;

import java.util.Collections;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.UniqueConstraint;

import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;

import play.db.jpa.Model;

/**
 * @author rjanixz
 *
 */
@Entity
@Audited
public class Role extends Model {

	@Column(unique=true, nullable=false)
	private String name;
	private String description;
	
	//Keeping DataBase's Namming Conventions
	@ManyToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name="RolePagePermission",
			joinColumns={@JoinColumn(name="roleId",referencedColumnName="id")},
			inverseJoinColumns={@JoinColumn(name="pagePermissionId",referencedColumnName="id")},
			uniqueConstraints = {@UniqueConstraint(columnNames = {"roleId","pagePermissionId"})})
	@Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED)
	private List<PagePermission> pagePermissions;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}

	public List<PagePermission> getPagePermissions() {
		if (pagePermissions == null) {
			pagePermissions = Collections.emptyList();
		}
		return pagePermissions;
	}

	public void setPagePermissions(List<PagePermission> pagePermissions) {
		this.pagePermissions = pagePermissions;
	}
	
}
